﻿/*
 * Created by SharpDevelop.
 * User: yeang-shing.then
 * Date: 11/16/2012
 * Time: 10:36 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace KCLIMTrading
{
	/// <summary>
	/// Dump Console output to a Windows control.
	/// </summary>
	/// <remarks>
	/// Improve: http://www.codeproject.com/Articles/13345/DbMon-NET-A-simple-NET-OutputDebugString-capturer
	/// </remarks>
	/// <seealso cref="http://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/">How to redirect the Console's output to a TextBox in C#</seealso>
	public class ConsoleStreamWriter: TextWriter
	{
		private Control control;
		private ToolStripStatusLabel statusLabel;
		public ConsoleStreamWriter(TextBox sender)
		{
			this.control = sender;
		}
		public ConsoleStreamWriter(ToolStripStatusLabel sender)
		{
			this.statusLabel = sender;
		}
		public override void Write(char value)
		{
			base.Write(value);
			if(control != null)
			{
				if(control is TextBox)
				{
					(control as TextBox).AppendText(value.ToString());
				}
				else
				{
					control.Text = value.ToString();
				}
			}
			
			//no meaning
//			if(statusLabel != null)
//			{
//				statusLabel.Text = value.ToString();
//			}
		}
		public override void WriteLine(string value)
		{
			base.WriteLine();
			if(control != null)
			{
				if(control is TextBox)
				{
					(control as TextBox).AppendText(value.ToString());
				}
				else
				{
					control.Text = value.ToString();
				}
			}
			
			if(statusLabel != null)
			{
				statusLabel.Text = value.ToString();
			}
		}
		public override Encoding Encoding {
			get {return System.Text.Encoding.UTF8;}
		}
	}
}